package com.zhijian.medical.util.redis;


import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

@Component
@ConfigurationProperties(prefix = "jedis")
@Data
public class RedisConfig {

    private String host;

    private int port;

    private String password;

    private static JedisPool jedisPool = null;

    public synchronized void initJedisPool() {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(10000);
        jedisPoolConfig.setMaxIdle(1000);
        jedisPoolConfig.setMaxWaitMillis(100 * 1000);
        jedisPoolConfig.setTestOnBorrow(false);
        jedisPool = new JedisPool(jedisPoolConfig, host, port, 10000, password);
    }

    public synchronized Jedis getResourceJedis() {
        try {
            if (jedisPool == null) {
                initJedisPool();
            }
            return jedisPool.getResource();
        } catch (Exception e) {
            initJedisPool();
        }
        return null;
    }
}
